using Logger7.Data;

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

using System.Security.Claims;

namespace Logger7.Pages.Logger;

public class ListModel : PageModel
{
    [BindProperty(SupportsGet = true)]
    public int PageNo { get; set; } = 0;
    [BindProperty(SupportsGet = true)]
    public int PageSize { get; set; } = 50;
    [BindProperty(SupportsGet = true)]
    public string? SearchText { get; set; }
    [BindProperty(SupportsGet = true)]
    public string? CategoryName { get; set; }
    public List<TLogger> Items { get; set; } = default!;
    public async Task<IActionResult> OnGetAsync([FromServices] SqlServerLoggerProcessor p)
    {
        string? userExt = ((ClaimsIdentity?)User.Identity)?.FindFirst("Ext")?.Value;
        if (PageNo == -1)
        {
            ViewData["Count"] = await p.DeleteAsync();
            PageNo = 0;
        }
        Items = await p.ReadAsync(PageNo, PageSize, userExt, CategoryName, SearchText);
        return Page();
    }
}
